package com.jinzhi.jzweb.controller;


import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.DateTime;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzweb.domain.JazInterviewDO;
import com.jinzhi.jzweb.domain.vo.JazUserDemandVO;
import com.jinzhi.jzweb.domain.vo.userAunt.UserVO;
import com.jinzhi.jzweb.service.JazInterviewService;
import com.jinzhi.jzweb.service.JazOrderTakingService;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazUserDemandDO;
import com.jinzhi.jzweb.service.JazUserDemandService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 用户需求管理
 * </pre>
 * <small> 2023-04-18 09:33:30 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazUserDemand")
public class JazUserDemandController extends BaseController {
	@Autowired
	private JazUserDemandService jazUserDemandService;
	@Autowired
	UserService userService;
	@Autowired
	private JazInterviewService jazInterviewService;
	@Autowired
	private JazOrderTakingService jazOrderTakingService;

	@GetMapping()
	@RequiresPermissions("jzweb:jazUserDemand:jazUserDemand")
	String JazUserDemand(){
	    return "jzweb/jazUserDemand/jazUserDemand";
	}
	@GetMapping("/infor/{type}")
	String JazUserDemand(@PathVariable("type") String type,Model model){
		UserDO userDO = userService.selectById(this.getUserId());
		model.addAttribute("name", userDO.getUsername());
		model.addAttribute("userId",userDO.getId());
		model.addAttribute("type",type);
		return "jzweb/jazUserDemand/add";
	}

	@GetMapping("/backstage")
	@RequiresPermissions("jzweb:backstage:list")
	String Backstage(){
		return "jzweb/backstage/index";
	}

	@GetMapping("/backstage/info/{type}/{id}")
	@RequiresPermissions("jzweb:backstage:list")
	String BackstageInfo(@PathVariable("id") Long id,@PathVariable("type") String type,Model model){
		JazUserDemandDO jazUserDemandBackgroundDO=null;
		if(!"add".equals(type)){
			Result<JazUserDemandDO>	jazUserDemandBackgroundDOs=jazUserDemandService.selectDemandById(id);
			jazUserDemandBackgroundDO=jazUserDemandBackgroundDOs.getData();
		}
		model.addAttribute("myData", jazUserDemandBackgroundDO);
		model.addAttribute("id", id);
		model.addAttribute("type", type);
		return "jzweb/backstage/info";
	}


	@GetMapping("/interviewInvitation")
	@RequiresPermissions("jzweb:interviewInvitation:list")
	String InterviewInvitation(){
	    return "jzweb/interviewInvitation/index";
	}

	@GetMapping("/interviewInvitation/info/{id}/{infoType}")
	@RequiresPermissions("jzweb:interviewInvitation:info")
	String InterviewInvitationInfo(@PathVariable("id") Long id,@PathVariable("infoType") String infoType,Model model){
		model.addAttribute("myData", jazInterviewService.getUserInterviewById(id));
		model.addAttribute("userId",id);
		model.addAttribute("infoType",infoType);
	    return "jzweb/interviewInvitation/info";
	}

	@GetMapping("/backstage/interviewInvitation")
	@RequiresPermissions("jzweb:backstageInterviewInvitation:list")
	String BackstageInterviewInvitation(){
		return "jzweb/backstageInterviewInvitation/index";
	}
	@GetMapping("/backstage/interviewInvitation/info/{demandId}/{inviteeId}/{infoType}")
	String backstageInterviewInvitationInfo(@PathVariable("demandId") Long demandId,@PathVariable("inviteeId") Long inviteeId,@PathVariable("infoType") String infoType,Model model){
		JazInterviewDO mydata= jazInterviewService.backgroundGetInterview(demandId,inviteeId);
	    mydata.setUserList(null==mydata.getUserList()?new ArrayList<UserVO>():mydata.getUserList());
		model.addAttribute("myData",mydata);
		model.addAttribute("userId",demandId);
		model.addAttribute("inviteeId",inviteeId);
		model.addAttribute("infoType",infoType);
		return "jzweb/backstageInterviewInvitation/info";
	}

	@GetMapping("/takeOrders")
	@RequiresPermissions("jzweb:takeOrders:list")
	String TakeOrders(){
		return "jzweb/takeOrders/index";
	}
	@GetMapping("/takeOrders/info/{id}/{infoType}")
	String takeOrdersInfo(@PathVariable("id") Long id,@PathVariable("infoType") String infoType,Model model){
		model.addAttribute("myData", jazOrderTakingService.getReceiverOrderById(id));
		model.addAttribute("userId",id);
		model.addAttribute("infoType",infoType);
		return "jzweb/takeOrders/info";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:jazUserDemand:jazUserDemand")
	public Result<?> list(JazUserDemandVO jazUserDemandVO,String current,String size){
		// 查询列表数据
		Page<JazUserDemandVO> jazUserDemandVOPage = jazUserDemandService.selectAll(jazUserDemandVO, Integer.valueOf(current), Integer.valueOf(size));
		List list = new ArrayList();
		for (int i = 0; i < jazUserDemandVOPage.getRecords().size(); i++) {
			JazUserDemandVO obj = (JazUserDemandVO) jazUserDemandVOPage.getRecords().get(i);
			obj.setCreateTime(DateTime.of(obj.getCreateTime()));
			list.add(obj);
		}
		jazUserDemandVOPage.setRecords(list);
		return Result.ok(jazUserDemandVOPage);
	}


	@GetMapping("/add")
	@RequiresPermissions("jzweb:jazUserDemand:add")
	String add(){
	    return "jzweb/jazUserDemand/add";
	}

	@Log(value = "添加用户需求信息",sysModule = 3,operationType = 1)
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:jazUserDemand:add")
	public Result<String> save( JazUserDemandVO jazUserDemandVO){
		JazUserDemandDO jazUserDemandDO = new JazUserDemandDO();
		try {
			BeanUtils.copyProperties(jazUserDemandDO,jazUserDemandVO);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		jazUserDemandDO.setSysUserid(getUserId());
		jazUserDemandDO.setCreateTime(new DateTime());
		jazUserDemandService.insert(jazUserDemandDO);
		return Result.ok();
	}


	@GetMapping("/info/{id}")
	@RequiresPermissions("jzweb:jazUserDemand:jazUserDemand")
	String info(@PathVariable("id") Long id,Model model){
		JazUserDemandVO jazUserDemandVO = jazUserDemandService.selectDemandDetail(id);
		jazUserDemandVO.setCreateTime(DateTime.of(jazUserDemandVO.getCreateTime()));
		jazUserDemandVO.setWorkDate(DateTime.of(jazUserDemandVO.getWorkDate()));
		jazUserDemandVO.setEndDate(DateTime.of(jazUserDemandVO.getEndDate()));
		model.addAttribute("jazUserDemandVO", jazUserDemandVO);
		model.addAttribute("id",id);
		return "jzweb/jazUserDemand/info";
	}

	@Log(value = "用户需求审核",sysModule = 3,operationType = 3)
	@ResponseBody
	@PostMapping("/examine")
	@RequiresPermissions("jzweb:jazUserDemand:edit")
		public Result<String> examine( JazUserDemandVO jazUserDemandVO){
		//获取数据审核状态
		JazUserDemandDO jazUserDemandDO1 = jazUserDemandService.selectById(jazUserDemandVO.getId());
		if(jazUserDemandDO1.getAuditStatus() == null){
			return Result.fail("审核状态不能为空！");
		}
		if(jazUserDemandDO1.getAuditStatus().equals(0)){		//待审核
			JazUserDemandDO jazUserDemandDO = jazUserDemandService.selectById(jazUserDemandVO.getId());
			jazUserDemandDO.setAuditStatus(jazUserDemandVO.getAuditStatus());
			if(jazUserDemandVO.getAuditResult() == null){
				return Result.fail("请选择审核结果！");
			}
			if(jazUserDemandVO.getAuditResult().equals(0)){		//审核通过
				jazUserDemandDO.setAuditResult(0);
				jazUserDemandDO.setAuditStatus(1);
			}else if(jazUserDemandVO.getAuditResult().equals(1)){	//审核拒绝
				jazUserDemandDO.setAuditResult(1);
				jazUserDemandDO.setAuditStatus(0);
				jazUserDemandDO.setRefuseReason(jazUserDemandVO.getRefuseReason());
			}else {
				return Result.fail("请选择审核结果！");
			}
			jazUserDemandService.updateById(jazUserDemandDO);
		}else if(jazUserDemandDO1.getAuditStatus().equals(1)){	//已审核
			JazUserDemandDO jazUserDemandDO = jazUserDemandService.selectById(jazUserDemandVO.getId());
			jazUserDemandDO.setAuditStatus(jazUserDemandVO.getAuditStatus());
			if(jazUserDemandVO.getShowStatus() == null){
				return Result.fail("请选择展示状态！");
			}
			if(jazUserDemandVO.getShowStatus().equals(0)){		//展示
				jazUserDemandDO.setShowStatus(0);
			}else if(jazUserDemandVO.getShowStatus().equals(1)){	//不展示
				jazUserDemandDO.setShowStatus(1);
			}else {
				return Result.fail("请选择展示状态！");
			}
			jazUserDemandService.updateById(jazUserDemandDO);
		}else {
			return Result.fail("审核状态不能为空！");
		}

		return Result.ok();
	}



	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:jazUserDemand:edit")
	String edit(@PathVariable("id") Long id,Model model){
		JazUserDemandVO jazUserDemandVO = jazUserDemandService.selectDemandDetail(id);
		model.addAttribute("jazUserDemandVO", jazUserDemandVO);
	    return "jzweb/jazUserDemand/edit";
	}
	
	@Log(value = "编辑并更新用户需求",sysModule = 3,operationType = 3)
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:jazUserDemand:edit")
	public Result<String>  update( JazUserDemandVO jazUserDemandVO){
		JazUserDemandDO jazUserDemandDO = new JazUserDemandDO();
		try {
			BeanUtils.copyProperties(jazUserDemandDO,jazUserDemandVO);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		jazUserDemandDO.setSysUserid(getUserId());
		jazUserDemandDO.setUpdateTime(new DateTime());
		boolean update = jazUserDemandService.updateById(jazUserDemandDO);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log(value = "删除用户需求数据",sysModule = 3,operationType = 2)
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazUserDemand:remove")
	public Result<String>  remove( Long id){
		jazUserDemandService.deleteById(id);
        return Result.ok();
	}
	
}
